package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.entity.RoleInfo;
import com.southwind.entity.UserInfo;
import com.southwind.exception.VideoException;
import com.southwind.result.ResponseEnum;
import com.southwind.service.RoleInfoService;
import com.southwind.service.UserInfoService;
import com.southwind.util.PageUtils;
import com.southwind.util.ResultVOUtil;
import com.southwind.vo.ResultVO;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2023-10-25
 */
//@Controller
@RestController
@RequestMapping("/v1")
public class UserInfoController {

        @Autowired
        private UserInfoService userInfoService;
        @Autowired
        private RoleInfoService roleInfoService;

        //分页查询用户
        @GetMapping("/list/{page}/{size}")
        @ResponseBody
        public ResultVO list(
                @PathVariable("page") Integer page,
                @PathVariable("size") Integer size
        ) {

            Page<UserInfo> page1 = new Page<>();
            Page<UserInfo> page2 = this.userInfoService.page(page1);
            ArrayList<UserInfo> list = new ArrayList<>();
            for (UserInfo record : page2.getRecords()) {
                UserInfo userInfo = new UserInfo();
                BeanUtils.copyProperties(record, userInfo);
                list.add(userInfo);
            }
            return ResultVOUtil.success(list);
        }

//        根据id查找用户角色

            @GetMapping("/findById/{id}")
    public ResultVO findById(
            @PathVariable("id") Long id
        ) throws VideoException {
            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
            queryWrapper.eq("id",id);
            UserInfo user = userInfoService.getOne(queryWrapper);
            if(user==null) throw new VideoException(ResponseEnum.USER_NULL.getMsg());
            return ResultVOUtil.success(user);
        }

        @PutMapping("/delete/{id}")
    public ResultVO delete( @PathVariable("id") Integer id){
            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",id);
            boolean remove = this.userInfoService.remove(queryWrapper);
            return ResultVOUtil.success(remove);

        }

    //根据用户id查询角色
    @GetMapping("/findRoleById/{id}")
        public ResultVO findRoleById(@PathVariable("id") Integer id) throws VideoException {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        UserInfo user = this.userInfoService.getOne(queryWrapper);
        if(user==null) throw new VideoException(ResponseEnum.USER_NULL.getMsg());
        String roleId = user.getRoleId();
        RoleInfo role = roleInfoService.getById(roleId);
        return ResultVOUtil.success(role);
    }







}

